#coding: utf8

import sys 
import os
import os.path 

cur_path = os.path.dirname(__file__)
parent_path = os.path.abspath(os.path.join(cur_path, '..'))
sys.path.append(parent_path)




import time
import pymongo
import logging
import logging.config
import json

from topapi import *



logging.config.fileConfig('./conf/log.conf')
slog = logging.getLogger('root')
mongo = pymongo.Connection('127.0.0.1:57324')

def process_a_user(user, retry=2):
    slog.info('process new user: %s' %(repr(user)))
    try:
        incobj = Increment() 
        res = incobj.customer_permit(user['top_session'])
        slog.info('add permit increment for user success[session: %s][%s]' %(user['top_session'], repr(res)))
    except Exception as e:
        slog.warning('add permit increment for user fail[session: %s]: %s' %(user['top_session'], repr(e)))


def start(db, filename):
    for i in open(filename):
        nick = i.strip()
        user = db.user.find_one({'_id':nick}, fields=['top_session', ])
        print user
        process_a_user(user)
        time.sleep(0.5)



if __name__ == '__main__':
    start(mongo.taogold, sys.argv[1])

